home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr16.lha / vector.lisp < prev    next >
Lisp/Scheme  |  1993-01-07  |  40KB  |  1,084 lines

  1. ;;;-*-Mode:LISP; Package:(PCL LISP 1000); Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27. ;;; Permutation vectors.
  28. ;;;
  29.  
  30. (in-package :pcl)
  31.  
  32. (defmacro instance-slot-index (wrapper slot-name)
  33.   `(let ((pos 0))
  34.      (declare (fixnum pos))
  35.      (block loop
  36.        (dolist (sn (wrapper-instance-slots-layout ,wrapper))
  37.      (when (eq ,slot-name sn) (return-from loop pos))
  38.      (incf pos)))))
  39.  
  40.  
  41. ;;;
  42. ;;;
  43. ;;;
  44. (defun pv-cache-limit-fn (nlines)
  45.   (default-limit-fn nlines))
  46.  
  47. (defstruct (pv-table
  48.          (:predicate pv-tablep)
  49.          (:constructor make-pv-table-internal
  50.                (slot-name-lists call-list)))
  51.   (cache nil :type (or cache null))
  52.   (pv-size 0 :type fixnum)
  53.   (slot-name-lists nil :type list)
  54.   (call-list nil :type list))
  55.  
  56. (defvar *initial-pv-table* (make-pv-table-internal nil nil))
  57.  
  58. ; help new slot-value-using-class methods affect fast iv access
  59. (defvar *all-pv-table-list* nil) 
  60.  
  61. (defun make-pv-table (&key slot-name-lists call-list)
  62.   (let ((pv-table (make-pv-table-internal slot-name-lists call-list)))
  63.     (push pv-table *all-pv-table-list*)
  64.     pv-table))
  65.  
  66. (defun make-pv-table-type-declaration (var)
  67.   `(type pv-table ,var))
  68.  
  69. (defvar *slot-name-lists-inner* (make-hash-table :test #'equal))
  70. (defvar *slot-name-lists-outer* (make-hash-table :test #'equal))
  71.  
  72. ;entries in this are lists of (table . pv-offset-list)
  73. (defvar *pv-key-to-pv-table-table* (make-hash-table :test 'equal))
  74.  
  75. (defun intern-pv-table (&key slot-name-lists call-list)
  76.   (let ((new-p nil))
  77.     (flet ((inner (x)
  78.          (or (gethash x *slot-name-lists-inner*)
  79.          (setf (gethash x *slot-name-lists-inner*) (copy-list x))))
  80.        (outer (x)
  81.          (or (gethash x *slot-name-lists-outer*)
  82.          (setf (gethash x *slot-name-lists-outer*)
  83.                (let ((snl (copy-list (cdr x)))
  84.                  (cl (car x)))
  85.              (setq new-p t)
  86.              (make-pv-table :slot-name-lists snl
  87.                     :call-list cl))))))
  88.     (let ((pv-table (outer (mapcar #'inner (cons call-list slot-name-lists)))))
  89.       (when new-p
  90.     (let ((pv-index 1))
  91.       (dolist (slot-name-list slot-name-lists)
  92.         (dolist (slot-name (cdr slot-name-list))
  93.           (note-pv-table-reference slot-name pv-index pv-table)
  94.           (incf pv-index)))
  95.       (dolist (gf-call call-list)
  96.         (note-pv-table-reference gf-call pv-index pv-table)
  97.         (incf pv-index))
  98.       (setf (pv-table-pv-size pv-table) pv-index)))
  99.       pv-table))))
  100.  
  101. (defun note-pv-table-reference (ref pv-offset pv-table)
  102.   (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
  103.     (when (listp entry)
  104.       (let ((table-entry (assq pv-table entry)))
  105.     (when (and (null table-entry)
  106.            (> (length entry) 8))
  107.       (let ((new-table-table (make-hash-table :size 16 :test 'eq)))
  108.         (dolist (table-entry entry)
  109.           (setf (gethash (car table-entry) new-table-table)
  110.             (cdr table-entry)))
  111.         (setf (gethash ref *pv-key-to-pv-table-table*) new-table-table)))
  112.     (when (listp entry)
  113.       (if (null table-entry)
  114.           (let ((new (cons pv-table pv-offset)))
  115.         (if (consp entry)
  116.             (push new (cdr entry))
  117.             (setf (gethash ref *pv-key-to-pv-table-table*) (list new))))
  118.           (push pv-offset (cdr table-entry)))
  119.       (return-from note-pv-table-reference nil))))
  120.     (let ((list (gethash pv-table entry)))
  121.       (if (consp list)
  122.       (push pv-offset (cdr list))
  123.       (setf (gethash pv-table entry) (list pv-offset)))))
  124.   nil)
  125.  
  126. (defun map-pv-table-references-of (ref function)
  127.   (let ((entry (gethash ref *pv-key-to-pv-table-table*)))
  128.     (if (listp entry)
  129.     (dolist (table+pv-offset-list entry)
  130.       (funcall function
  131.            (car table+pv-offset-list) (cdr table+pv-offset-list)))
  132.     (maphash function entry)))
  133.   ref)
  134.  
  135.  
  136. (defvar *pvs* (make-hash-table :test #'equal))
  137.  
  138. (defun optimize-slot-value-by-class-p (class slot-name type)
  139.   (or (not (eq *boot-state* 'complete))
  140.       (let ((slotd (find-slot-definition class slot-name)))
  141.     (and slotd 
  142.          (slot-accessor-std-p slotd type)))))
  143.  
  144. (defun compute-pv-slot (slot-name wrapper class class-slots class-slot-p-cell)
  145.   (if (symbolp slot-name)
  146.       (when (optimize-slot-value-by-class-p class slot-name 'all)
  147.     (or (instance-slot-index wrapper slot-name)
  148.         (let ((cell (assq slot-name class-slots)))
  149.           (when cell
  150.         (setf (car class-slot-p-cell) t)
  151.         cell))))
  152.       (when (consp slot-name)
  153.     (dolist (type '(reader writer) nil)
  154.       (when (eq (car slot-name) type)
  155.         (return
  156.           (let* ((gf-name (cadr slot-name))
  157.              (gf (gdefinition gf-name))
  158.              (location 
  159.               (when (eq *boot-state* 'complete)
  160.             (accessor-values1 gf type class))))
  161.         (when (consp location)
  162.           (setf (car class-slot-p-cell) t))
  163.         location)))))))
  164.  
  165. (defun compute-pv (slot-name-lists wrappers)
  166.   (unless (listp wrappers) (setq wrappers (list wrappers)))
  167.   (let* ((not-simple-p-cell (list nil))
  168.      (elements
  169.       (gathering1 (collecting)
  170.         (iterate ((slot-names (list-elements slot-name-lists)))
  171.           (when slot-names
  172.         (let* ((wrapper     (pop wrappers))
  173.                (class       (wrapper-class* wrapper))
  174.                (class-slots (wrapper-class-slots wrapper)))
  175.           (dolist (slot-name (cdr slot-names))
  176.             (gather1
  177.              (compute-pv-slot slot-name wrapper class 
  178.                       class-slots not-simple-p-cell)))))))))
  179.     (if (car not-simple-p-cell)
  180.     (make-permutation-vector (cons t elements))
  181.     (or (gethash elements *pvs*)
  182.         (setf (gethash elements *pvs*)
  183.           (make-permutation-vector (cons nil elements)))))))
  184.  
  185. (defun compute-calls (call-list wrappers)
  186.   (declare (ignore call-list wrappers))
  187.   #||
  188.   (map 'vector
  189.        #'(lambda (call)
  190.        (compute-emf-from-wrappers call wrappers))
  191.        call-list)
  192.   ||#  
  193.   '#())
  194.  
  195. #|| ; Need to finish this, then write the maintenance functions.
  196. (defun compute-emf-from-wrappers (call wrappers)
  197.   (when call
  198.     (destructuring-bind (gf-name nreq restp arg-info) call
  199.       (if (eq gf-name 'make-instance)
  200.       (error "should not get here") ; there is another mechanism for this.
  201.       #'(lambda (&rest args)
  202.           (if (not (eq *boot-state* 'complete))
  203.           (apply (gdefinition gf-name) args)
  204.           (let* ((gf (gdefinition gf-name))
  205.              (arg-info (arg-info-reader gf))
  206.              (classes '?)
  207.              (types '?)
  208.              (emf (cache-miss-values-internal gf arg-info 
  209.                               wrappers classes types 
  210.                               'caching)))
  211.             (update-all-pv-tables call wrappers emf)
  212.             #+copy-&rest-arg (setq args (copy-list args))
  213.             (invoke-emf emf args))))))))
  214. ||#
  215.  
  216. (defun make-permutation-vector (indexes)
  217.   (make-array (length indexes) :initial-contents indexes))
  218.  
  219. (defun pv-table-lookup (pv-table pv-wrappers)
  220.   (let* ((slot-name-lists (pv-table-slot-name-lists pv-table))
  221.      (call-list (pv-table-call-list pv-table))
  222.      (cache (or (pv-table-cache pv-table)
  223.             (setf (pv-table-cache pv-table)
  224.               (get-cache (- (length slot-name-lists)
  225.                     (count nil slot-name-lists))
  226.                      t
  227.                      #'pv-cache-limit-fn
  228.                      2)))))
  229.     (or (probe-cache cache pv-wrappers)
  230.     (let* ((pv (compute-pv slot-name-lists pv-wrappers))
  231.            (calls (compute-calls call-list pv-wrappers))
  232.            (pv-cell (cons pv calls))
  233.            (new-cache (fill-cache cache pv-wrappers pv-cell)))
  234.       (unless (eq new-cache cache)
  235.         (setf (pv-table-cache pv-table) new-cache)
  236.         (free-cache cache))
  237.       pv-cell))))
  238.  
  239. (defun make-pv-type-declaration (var)
  240.   `(type simple-vector ,var))
  241.  
  242. (defvar *empty-pv* #())
  243.  
  244. (defmacro pvref (pv index)
  245.   `(svref ,pv ,index))
  246.  
  247. (defmacro copy-pv (pv)
  248.   `(copy-seq ,pv))
  249.  
  250. (defun make-calls-type-declaration (var)
  251.   `(type simple-vector ,var))
  252.  
  253. (defmacro callsref (calls index)
  254.   `(svref ,calls ,index))
  255.  
  256. (defvar *pv-table-cache-update-info* nil)
  257.  
  258. ;called by: 
  259. ;(method shared-initialize :after (structure-class t))
  260. ;update-slots
  261. (defun update-pv-table-cache-info (class)
  262.   (let ((slot-names-for-pv-table-update nil)
  263.     (new-icui nil))
  264.     (dolist (icu *pv-table-cache-update-info*)
  265.       (if (eq (car icu) class)
  266.       (pushnew (cdr icu) slot-names-for-pv-table-update)
  267.       (push icu new-icui)))
  268.     (setq *pv-table-cache-update-info* new-icui)
  269.     (when slot-names-for-pv-table-update
  270.       (update-all-pv-table-caches class slot-names-for-pv-table-update))))
  271.  
  272. (defun update-all-pv-table-caches (class slot-names)
  273.   (let* ((cwrapper (class-wrapper class))
  274.      (class-slots (wrapper-class-slots cwrapper))
  275.      (class-slot-p-cell (list nil))
  276.      (new-values (mapcar #'(lambda (slot-name)
  277.                  (cons slot-name
  278.                        (compute-pv-slot 
  279.                     slot-name cwrapper class 
  280.                     class-slots class-slot-p-cell)))
  281.                  slot-names))
  282.      (pv-tables nil))
  283.     (dolist (slot-name slot-names)
  284.       (map-pv-table-references-of
  285.        slot-name
  286.        #'(lambda (pv-table pv-offset-list)
  287.        (declare (ignore pv-offset-list))
  288.        (pushnew pv-table pv-tables))))
  289.     (dolist (pv-table pv-tables)
  290.       (let* ((cache (pv-table-cache pv-table))
  291.          (slot-name-lists (pv-table-slot-name-lists pv-table))
  292.          (pv-size (pv-table-pv-size pv-table))
  293.          (pv-map (make-array pv-size)))
  294.     (let ((map-index 1)(param-index 0))
  295.       (dolist (slot-name-list slot-name-lists)
  296.         (dolist (slot-name (cdr slot-name-list))
  297.           (let ((a (assoc slot-name new-values)))
  298.         (setf (svref pv-map map-index)
  299.               (and a (cons param-index (cdr a)))))
  300.           (incf map-index))
  301.         (incf param-index)))
  302.     (when cache
  303.       (map-cache #'(lambda (wrappers pv-cell)
  304.              (setf (car pv-cell)
  305.                    (update-slots-in-pv wrappers (car pv-cell)
  306.                            cwrapper pv-size pv-map)))
  307.              cache))))))
  308.  
  309. (defun update-slots-in-pv (wrappers pv cwrapper pv-size pv-map)
  310.   (if (not (if (atom wrappers)
  311.            (eq cwrapper wrappers)
  312.            (dolist (wrapper wrappers nil)
  313.          (when (eq wrapper cwrapper)
  314.            (return t)))))
  315.       pv
  316.       (let* ((old-intern-p (listp (pvref pv 0)))
  317.          (new-pv (if old-intern-p
  318.              (copy-pv pv)
  319.              pv))
  320.          (new-intern-p t))
  321.     (if (atom wrappers)
  322.         (dotimes (i pv-size)
  323.           (when (consp (let ((map (svref pv-map i)))
  324.                  (if map
  325.                  (setf (pvref new-pv i) (cdr map))
  326.                  (pvref new-pv i))))
  327.         (setq new-intern-p nil)))
  328.         (let ((param 0))
  329.           (dolist (wrapper wrappers)
  330.         (when (eq wrapper cwrapper)
  331.           (dotimes (i pv-size)
  332.             (when (consp (let ((map (svref pv-map i)))
  333.                    (if (and map (= (car map) param))
  334.                        (setf (pvref new-pv i) (cdr map))
  335.                        (pvref new-pv i))))
  336.               (setq new-intern-p nil))))
  337.         (incf param))))
  338.     (when new-intern-p
  339.       (setq new-pv (let ((list-pv (coerce pv 'list)))
  340.              (or (gethash (cdr list-pv) *pvs*)
  341.                  (setf (gethash (cdr list-pv) *pvs*)
  342.                    (if old-intern-p
  343.                        new-pv
  344.                        (make-permutation-vector list-pv)))))))
  345.     new-pv)))
  346.  
  347.  
  348. (defun maybe-expand-accessor-form (form required-parameters slots env)
  349.   (let* ((fname (car form))
  350.      #||(len (length form))||#
  351.      (gf (if (symbolp fname)
  352.          (unencapsulated-fdefinition fname)
  353.          (gdefinition fname))))
  354.     (macrolet ((maybe-optimize-reader ()
  355.          `(let ((parameter
  356.              (can-optimize-access1 (cadr form)
  357.                            required-parameters env)))
  358.            (when parameter
  359.              (optimize-reader slots parameter gf-name form))))
  360.            (maybe-optimize-writer ()
  361.          `(let ((parameter
  362.              (can-optimize-access1 (caddr form)
  363.                            required-parameters env)))
  364.            (when parameter
  365.              (optimize-writer slots parameter gf-name form)))))
  366.       (unless (and (consp (cadr form))
  367.            (eq 'instance-accessor-parameter (caadr form)))
  368.     (or #||
  369.         (cond ((and (= len 2) (symbolp fname))
  370.            (let ((gf-name (gethash fname *gf-declared-reader-table*)))
  371.              (when gf-name
  372.                (maybe-optimize-reader))))
  373.           ((= len 3)
  374.            (let ((gf-name (gethash fname *gf-declared-writer-table*)))
  375.              (when gf-name
  376.                (maybe-optimize-writer)))))
  377.         ||#
  378.         (when (and (eq *boot-state* 'complete)
  379.                (generic-function-p gf))
  380.           (let ((methods (generic-function-methods gf)))
  381.         (when methods
  382.           (let* ((gf-name (generic-function-name gf))
  383.              (arg-info (gf-arg-info gf))
  384.              (metatypes (arg-info-metatypes arg-info))
  385.              (nreq (length metatypes))
  386.              (applyp (arg-info-applyp arg-info)))
  387.             (when (null applyp)
  388.               (cond ((= nreq 1)
  389.                  (when (some #'standard-reader-method-p methods)
  390.                    (maybe-optimize-reader)))
  391.                 ((and (= nreq 2)
  392.                   (consp gf-name)
  393.                   (eq (car gf-name) 'setf))
  394.                  (when (some #'standard-writer-method-p methods)
  395.                    (maybe-optimize-writer))))))))))))))
  396.  
  397. (defun optimize-generic-function-call (form required-parameters env slots calls)
  398.   (declare (ignore required-parameters env slots calls))
  399.   (or (and (eq (car form) 'make-instance)
  400.        (expand-make-instance-form form))
  401.       #||
  402.       (maybe-expand-accessor-form form required-parameters slots env)
  403.       (let* ((fname (car form))
  404.          (len (length form))
  405.          (gf (if (symbolp fname)
  406.              (and (fboundp fname)
  407.               (unencapsulated-fdefinition fname))
  408.              (and (gboundp fname)
  409.               (gdefinition fname))))
  410.          (gf-name (and (fsc-instance-p gf)
  411.                (if (early-gf-p gf)
  412.                    (early-gf-name gf)
  413.                    (generic-function-name gf)))))
  414.     (when gf-name
  415.       (multiple-value-bind (nreq restp)
  416.           (get-generic-function-info gf)
  417.         (optimize-gf-call slots calls form nreq restp env))))
  418.       ||#
  419.       form))
  420.  
  421.  
  422.  
  423. (defun can-optimize-access (form required-parameters env)
  424.   (let ((type (ecase (car form)
  425.         (slot-value 'reader)
  426.         (set-slot-value 'writer)
  427.         (slot-boundp 'boundp)))
  428.     (var (cadr form))
  429.     (slot-name (eval (caddr form)))) ; known to be constant
  430.     (can-optimize-access1 var required-parameters env type slot-name)))
  431.  
  432. (defun can-optimize-access1 (var required-parameters env &optional type slot-name)
  433.   (when (and (consp var) (eq 'the (car var)))
  434.     (setq var (caddr var)))
  435.   (when (symbolp var)
  436.     (let* ((rebound? (caddr (variable-declaration 'variable-rebinding var env)))
  437.        (parameter-or-nil (car (memq (or rebound? var) required-parameters))))
  438.       (when parameter-or-nil
  439.     (let* ((class-name (caddr (variable-declaration 
  440.                    'class parameter-or-nil env)))
  441.            (class (find-class class-name nil)))
  442.       (when (or (not (eq *boot-state* 'complete))
  443.             (and class (not (class-finalized-p class))))
  444.         (setq class nil))
  445.       (when (and class-name (not (eq class-name 't)))
  446.         (when (or (null type)
  447.               (not (and class
  448.                 (memq *the-class-structure-object*
  449.                       (class-precedence-list class))))
  450.               (optimize-slot-value-by-class-p class slot-name type))
  451.           (cons parameter-or-nil (or class class-name)))))))))
  452.  
  453. (defun optimize-slot-value (slots sparameter form)
  454.   (if sparameter
  455.       (destructuring-bind (ignore ignore slot-name-form) form
  456.     (let ((slot-name (eval slot-name-form)))
  457.       (optimize-instance-access slots :read sparameter slot-name nil)))
  458.       `(accessor-slot-value ,@(cdr form))))
  459.  
  460. (defun optimize-set-slot-value (slots sparameter form)
  461.   (if sparameter
  462.       (destructuring-bind (ignore ignore slot-name-form new-value) form
  463.     (let ((slot-name (eval slot-name-form)))
  464.       (optimize-instance-access slots :write sparameter slot-name new-value)))
  465.       `(accessor-set-slot-value ,@(cdr form))))
  466.  
  467. (defun optimize-slot-boundp (slots sparameter form)
  468.   (if sparameter
  469.       (destructuring-bind (ignore ignore slot-name-form new-value) form
  470.     (let ((slot-name (eval slot-name-form)))
  471.       (optimize-instance-access slots :boundp sparameter slot-name new-value)))
  472.       `(accessor-slot-boundp ,@(cdr form))))
  473.  
  474. (defun optimize-reader (slots sparameter gf-name form)
  475.   (if sparameter
  476.       (optimize-accessor-call slots :read sparameter gf-name nil)
  477.       form))
  478.  
  479. (defun optimize-writer (slots sparameter gf-name form)
  480.   (if sparameter
  481.       (destructuring-bind (ignore ignore new-value) form
  482.     (optimize-accessor-call slots :write sparameter gf-name new-value))
  483.       form))
  484. ;;;
  485. ;;; The <slots> argument is an alist, the CAR of each entry is the name of
  486. ;;; a required parameter to the function.  The alist is in order, so the
  487. ;;; position of an entry in the alist corresponds to the argument's position
  488. ;;; in the lambda list.
  489. ;;; 
  490. (defun optimize-instance-access (slots read/write sparameter slot-name new-value)
  491.   (let ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
  492.     (parameter (if (consp sparameter) (car sparameter) sparameter)))
  493.     (if (and (eq *boot-state* 'complete)
  494.          (classp class)
  495.          (memq *the-class-structure-object* (class-precedence-list class)))
  496.     (let ((slotd (find-slot-definition class slot-name)))
  497.       (ecase read/write
  498.         (:read
  499.          `(,(slot-definition-defstruct-accessor-symbol slotd) ,parameter))
  500.         (:write
  501.          `(setf (,(slot-definition-defstruct-accessor-symbol slotd) ,parameter)
  502.            ,new-value))
  503.         (:boundp
  504.          'T)))
  505.     (let* ((parameter-entry (assq parameter slots))
  506.            (slot-entry      (assq slot-name (cdr parameter-entry)))
  507.            (position (posq parameter-entry slots))
  508.            (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
  509.       (unless parameter-entry
  510.         (error "Internal error in slot optimization."))
  511.       (unless slot-entry
  512.         (setq slot-entry (list slot-name))
  513.         (push slot-entry (cdr parameter-entry)))
  514.       (push pv-offset-form (cdr slot-entry))
  515.       (ecase read/write
  516.         (:read
  517.          `(instance-read ,pv-offset-form ,parameter ,position 
  518.                      ',slot-name ',class))
  519.         (:write
  520.          `(let ((.new-value. ,new-value)) 
  521.             (instance-write ,pv-offset-form ,parameter ,position 
  522.                         ',slot-name ',class .new-value.)))
  523.         (:boundp
  524.          `(instance-boundp ,pv-offset-form ,parameter ,position 
  525.                        ',slot-name ',class)))))))
  526.  
  527. (defun optimize-accessor-call (slots read/write sparameter gf-name new-value)
  528.   (let* ((class (if (consp sparameter) (cdr sparameter) *the-class-t*))
  529.      (parameter (if (consp sparameter) (car sparameter) sparameter))
  530.      (parameter-entry (assq parameter slots))
  531.      (name (case read/write
  532.          (:read `(reader ,gf-name))
  533.          (:write `(writer ,gf-name))))
  534.      (slot-entry      (assoc name (cdr parameter-entry) :test #'equal))
  535.      (position (posq parameter-entry slots))
  536.      (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
  537.     (unless parameter-entry
  538.       (error "Internal error in slot optimization."))
  539.     (unless slot-entry
  540.       (setq slot-entry (list name))
  541.       (push slot-entry (cdr parameter-entry)))
  542.     (push pv-offset-form (cdr slot-entry))
  543.     (ecase read/write
  544.       (:read
  545.        `(instance-reader ,pv-offset-form ,parameter ,position ,gf-name ',class))
  546.       (:write
  547.        `(let ((.new-value. ,new-value)) 
  548.       (instance-writer ,pv-offset-form ,parameter ,position ,gf-name ',class
  549.                        .new-value.))))))
  550.  
  551. (defvar *unspecific-arg* '..unspecific-arg..)
  552.  
  553. (defun optimize-gf-call-internal (form slots env)
  554.   (when (and (consp form)
  555.          (eq (car form) 'the))
  556.     (setq form (caddr form)))
  557.   (or (and (symbolp form)
  558.        (let* ((rebound? (caddr (variable-declaration 'variable-rebinding
  559.                              form env)))
  560.           (parameter-or-nil (car (assq (or rebound? form) slots))))
  561.          (when parameter-or-nil
  562.            (let* ((class-name (caddr (variable-declaration 
  563.                       'class parameter-or-nil env))))
  564.          (when (and class-name (not (eq class-name 't)))
  565.            (position parameter-or-nil slots :key #'car))))))
  566.       (if (constantp form)
  567.       (let ((form (eval form)))
  568.         (if (symbolp form)
  569.         form
  570.         *unspecific-arg*))
  571.       *unspecific-arg*)))
  572.  
  573. (defun optimize-gf-call (slots calls gf-call-form nreq restp env)
  574.   (unless (eq (car gf-call-form) 'make-instance) ; needs more work
  575.     (let* ((args (cdr gf-call-form))
  576.        (all-args-p (eq (car gf-call-form) 'make-instance))
  577.        (non-required-args (nthcdr nreq args))
  578.        (required-args (ldiff args non-required-args))
  579.        (call-spec (list (car gf-call-form) nreq restp
  580.                 (mapcar #'(lambda (form)
  581.                     (optimize-gf-call-internal form slots env))
  582.                     (if all-args-p
  583.                     args
  584.                     required-args))))
  585.        (call-entry (assoc call-spec calls :test #'equal))
  586.        (pv-offset-form (list 'pv-offset ''.PV-OFFSET.)))
  587.       (unless (some #'integerp 
  588.             (let ((spec-args (cdr call-spec)))
  589.               (if all-args-p 
  590.               (ldiff spec-args (nthcdr nreq spec-args))
  591.               spec-args)))
  592.     (return-from optimize-gf-call nil))
  593.       (unless call-entry
  594.     (setq call-entry (list call-spec))
  595.     (push call-entry (cdr calls)))
  596.       (push pv-offset-form (cdr call-entry))
  597.       (if (eq (car call-spec) 'make-instance)
  598.       `(funcall (pv-ref .pv. ,pv-offset-form) ,@(cdr gf-call-form))
  599.       `(let ((.emf. (pv-ref .pv. ,pv-offset-form)))
  600.         (invoke-effective-method-function .emf. ,restp
  601.          ,@required-args ,@(when restp `((list ,@non-required-args)))))))))
  602.       
  603.  
  604. (define-walker-template pv-offset) ; These forms get munged by mutate slots.
  605. (defmacro pv-offset (arg) arg)
  606. (define-walker-template instance-accessor-parameter)
  607. (defmacro instance-accessor-parameter (x) x)
  608.  
  609. ;; It is safe for these two functions to be wrong.
  610. ;; They just try to guess what the most likely case will be.
  611. (defun generate-fast-class-slot-access-p (class-form slot-name-form)
  612.   (let ((class (and (constantp class-form) (eval class-form)))
  613.     (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
  614.     (and (eq *boot-state* 'complete)
  615.      (standard-class-p class)
  616.      (not (eq class *the-class-t*)) ; shouldn't happen, though.
  617.      (let ((slotd (find-slot-definition class slot-name)))
  618.        (and slotd (classp (slot-definition-allocation slotd)))))))
  619.  
  620. (defun skip-fast-slot-access-p (class-form slot-name-form type)
  621.   (let ((class (and (constantp class-form) (eval class-form)))
  622.     (slot-name (and (constantp slot-name-form) (eval slot-name-form))))
  623.     (and (eq *boot-state* 'complete)
  624.      (standard-class-p class)
  625.      (not (eq class *the-class-t*)) ; shouldn't happen, though.
  626.      (let ((slotd (find-slot-definition class slot-name)))
  627.        (and slotd (skip-optimize-slot-value-by-class-p class slot-name type))))))
  628.  
  629. (defun skip-optimize-slot-value-by-class-p (class slot-name type)
  630.   (let ((slotd (find-slot-definition class slot-name)))
  631.     (and slotd
  632.      (eq *boot-state* 'complete)
  633.      (not (slot-accessor-std-p slotd type)))))
  634.  
  635. (defmacro instance-read-internal (pv slots pv-offset default &optional type)
  636.   (unless (member type '(nil :instance :class :default))
  637.     (error "Illegal type argument to ~S: ~S" 'instance-read-internal type))
  638.   (if (eq type ':default)
  639.       default
  640.       (let* ((index (gensym))
  641.          (value index))
  642.     `(locally (declare #.*optimize-speed*)
  643.       (let ((,index (pvref ,pv ,pv-offset)))
  644.         (setq ,value (typecase ,index
  645.                ,@(when (or (null type) (eq type ':instance))
  646.                    `((fixnum (%instance-ref ,slots ,index))))
  647.                ,@(when (or (null type) (eq type ':class))
  648.                    `((cons (cdr ,index))))
  649.                (t ',*slot-unbound*)))
  650.         (if (eq ,value ',*slot-unbound*)
  651.         ,default
  652.         ,value))))))
  653.  
  654. (defmacro instance-read (pv-offset parameter position slot-name class)
  655.   (if (skip-fast-slot-access-p class slot-name 'reader)
  656.       `(accessor-slot-value ,parameter ,slot-name)
  657.       `(instance-read-internal .pv. ,(slot-vector-symbol position)
  658.     ,pv-offset (accessor-slot-value ,parameter ,slot-name)
  659.     ,(if (generate-fast-class-slot-access-p class slot-name)
  660.          ':class ':instance))))
  661.  
  662. (defmacro instance-reader (pv-offset parameter position gf-name class)
  663.   (declare (ignore class))
  664.   `(instance-read-internal .pv. ,(slot-vector-symbol position)
  665.     ,pv-offset 
  666.     (,gf-name (instance-accessor-parameter ,parameter))
  667.     :instance))
  668.  
  669. (defmacro instance-write-internal (pv slots pv-offset new-value default
  670.                       &optional type)
  671.   (unless (member type '(nil :instance :class :default))
  672.     (error "Illegal type argument to ~S: ~S" 'instance-write-internal type))
  673.   (if (eq type ':default)
  674.       default
  675.       (let* ((index (gensym)))
  676.     `(locally (declare #.*optimize-speed*)
  677.       (let ((,index (pvref ,pv ,pv-offset)))
  678.         (typecase ,index
  679.           ,@(when (or (null type) (eq type ':instance))
  680.           `((fixnum (setf (%instance-ref ,slots ,index) ,new-value))))
  681.           ,@(when (or (null type) (eq type ':class))
  682.           `((cons (setf (cdr ,index) ,new-value))))
  683.           (t ,default)))))))
  684.  
  685. (defmacro instance-write (pv-offset parameter position slot-name class new-value)
  686.   (if (skip-fast-slot-access-p class slot-name 'writer)
  687.       `(accessor-set-slot-value ,parameter ,slot-name ,new-value)
  688.       `(instance-write-internal .pv. ,(slot-vector-symbol position)
  689.     ,pv-offset ,new-value
  690.     (accessor-set-slot-value ,parameter ,slot-name ,new-value)
  691.     ,(if (generate-fast-class-slot-access-p class slot-name)
  692.          ':class ':instance))))
  693.  
  694. (defmacro instance-writer (pv-offset parameter position gf-name class new-value)
  695.   (declare (ignore class))
  696.   `(instance-write-internal .pv. ,(slot-vector-symbol position)
  697.     ,pv-offset ,new-value
  698.     (,(if (consp gf-name)
  699.       (get-setf-function-name gf-name)
  700.       gf-name)
  701.      (instance-accessor-parameter ,parameter)
  702.      ,new-value)
  703.     :instance))
  704.  
  705. (defmacro instance-boundp-internal (pv slots pv-offset default
  706.                        &optional type)
  707.   (unless (member type '(nil :instance :class :default))
  708.     (error "Illegal type argument to ~S: ~S" 'instance-boundp-internal type))
  709.   (if (eq type ':default)
  710.       default
  711.       (let* ((index (gensym)))
  712.     `(locally (declare #.*optimize-speed*)
  713.       (let ((,index (pvref ,pv ,pv-offset)))
  714.         (typecase ,index
  715.           ,@(when (or (null type) (eq type ':instance))
  716.           `((fixnum (not (eq (%instance-ref ,slots ,index) ',*slot-unbound*)))))
  717.           ,@(when (or (null type) (eq type ':class))
  718.           `((cons (not (eq (cdr ,index) ',*slot-unbound*)))))
  719.           (t ,default)))))))
  720.  
  721. (defmacro instance-boundp (pv-offset parameter position slot-name class)
  722.   (if (skip-fast-slot-access-p class slot-name 'boundp)
  723.       `(accessor-slot-boundp ,parameter ,slot-name)
  724.       `(instance-boundp-internal .pv. ,(slot-vector-symbol position)
  725.     ,pv-offset (accessor-slot-boundp ,parameter ,slot-name)
  726.     ,(if (generate-fast-class-slot-access-p class slot-name)
  727.          ':class ':instance))))
  728.  
  729. ;;;
  730. ;;; This magic function has quite a job to do indeed.
  731. ;;;
  732. ;;; The careful reader will recall that <slots> contains all of the optimized
  733. ;;; slot access forms produced by OPTIMIZE-INSTANCE-ACCESS.  Each of these is
  734. ;;; a call to either INSTANCE-READ or INSTANCE-WRITE.
  735. ;;;
  736. ;;; At the time these calls were produced, the first argument was specified as
  737. ;;; the symbol .PV-OFFSET.; what we have to do now is convert those pv-offset
  738. ;;; arguments into the actual number that is the correct offset into the pv.
  739. ;;;
  740. ;;; But first, oh but first, we sort <slots> a bit so that for each argument
  741. ;;; we have the slots in alphabetical order.  This canonicalizes the PV-TABLE's a
  742. ;;; bit and will hopefully lead to having fewer PV's floating around.  Even
  743. ;;; if the gain is only modest, it costs nothing.
  744. ;;;  
  745. (defun slot-name-lists-from-slots (slots calls)
  746.   (multiple-value-bind (slots calls)
  747.       (mutate-slots-and-calls slots calls)
  748.     (let* ((slot-name-lists
  749.         (mapcar #'(lambda (parameter-entry)
  750.             (cons nil (mapcar #'car (cdr parameter-entry))))
  751.             slots))
  752.        (call-list
  753.         (mapcar #'car calls)))
  754.       (dolist (call call-list)
  755.     (dolist (arg (cdr call))
  756.       (when (integerp arg)
  757.         (setf (car (nth arg slot-name-lists)) t))))
  758.       (setq slot-name-lists (mapcar #'(lambda (r+snl)
  759.                     (when (or (car r+snl) (cdr r+snl))
  760.                       r+snl))
  761.                     slot-name-lists))
  762.       (let ((cvt (apply #'vector
  763.             (let ((i -1))
  764.               (mapcar #'(lambda (r+snl)
  765.                       (when r+snl (incf i)))
  766.                   slot-name-lists)))))
  767.     (setq call-list (mapcar #'(lambda (call)
  768.                     (cons (car call) 
  769.                       (mapcar #'(lambda (arg)
  770.                               (if (integerp arg)
  771.                               (svref cvt arg)
  772.                               arg))
  773.                           (cdr call))))
  774.                 call-list)))
  775.       (values slot-name-lists call-list))))
  776.  
  777. (defun mutate-slots-and-calls (slots calls)
  778.   (let ((sorted-slots (sort-slots slots))
  779.     (sorted-calls (sort-calls (cdr calls)))
  780.     (pv-offset 0))  ; index 0 is for info
  781.     (dolist (parameter-entry sorted-slots)
  782.       (dolist (slot-entry (cdr parameter-entry))
  783.     (incf pv-offset)    
  784.     (dolist (form (cdr slot-entry))
  785.       (setf (cadr form) pv-offset))))
  786.     (dolist (call-entry sorted-calls)
  787.       (incf pv-offset)
  788.       (dolist (form (cdr call-entry))
  789.     (setf (cadr form) pv-offset)))
  790.     (values sorted-slots sorted-calls)))
  791.  
  792. (defun symbol-pkg-name (sym) 
  793.   (let ((pkg (symbol-package sym)))
  794.     (if pkg (package-name pkg) "")))
  795.  
  796. (defun symbol-lessp (a b)
  797.   (if (eq (symbol-package a)
  798.       (symbol-package b))
  799.       (string-lessp (symbol-name a)
  800.             (symbol-name b))
  801.       (string-lessp (symbol-pkg-name a)
  802.             (symbol-pkg-name b))))
  803.  
  804. (defun symbol-or-cons-lessp (a b)
  805.   (etypecase a
  806.     (symbol (etypecase b
  807.           (symbol (symbol-lessp a b))
  808.           (cons t)))
  809.     (cons   (etypecase b
  810.           (symbol nil)
  811.           (cons (if (eq (car a) (car b))
  812.             (symbol-or-cons-lessp (cdr a) (cdr b))
  813.             (symbol-or-cons-lessp (car a) (car b))))))))
  814.  
  815. (defun sort-slots (slots)
  816.   (mapcar #'(lambda (parameter-entry)
  817.           (cons (car parameter-entry)
  818.             (sort (cdr parameter-entry)    ;slot entries
  819.               #'symbol-or-cons-lessp
  820.               :key #'car)))
  821.       slots))
  822.  
  823. (defun sort-calls (calls)
  824.   (sort calls #'symbol-or-cons-lessp :key #'car))
  825.  
  826.  
  827. ;;;
  828. ;;; This needs to work in terms of metatypes and also needs to work for
  829. ;;; automatically generated reader and writer functions.
  830. ;;; -- Automatically generated reader and writer functions use this stuff too.
  831.  
  832. (defmacro pv-binding ((required-parameters slot-name-lists pv-table-symbol)
  833.               &body body)
  834.   (with-gathering ((slot-vars (collecting))
  835.            (pv-parameters (collecting)))
  836.     (iterate ((slots (list-elements slot-name-lists))
  837.           (required-parameter (list-elements required-parameters))
  838.           (i (interval :from 0)))
  839.       (when slots
  840.     (gather required-parameter pv-parameters)
  841.     (gather (slot-vector-symbol i) slot-vars)))
  842.     `(pv-binding1 (.pv. .calls. ,pv-table-symbol ,pv-parameters ,slot-vars)
  843.        ,@body)))
  844.  
  845. (defmacro pv-binding1 ((pv calls pv-table-symbol pv-parameters slot-vars) 
  846.                &body body)
  847.   `(pv-env (,pv ,calls ,pv-table-symbol ,pv-parameters)
  848.      (let (,@(mapcar #'(lambda (slot-var p) `(,slot-var (get-slots-or-nil ,p)))
  849.            slot-vars pv-parameters))
  850.     ,@body)))
  851.  
  852. ;This gets used only when the default make-method-lambda is overriden.
  853. (defmacro pv-env ((pv calls pv-table-symbol pv-parameters)
  854.           &rest forms)
  855.   `(let* ((.pv-table. ,pv-table-symbol)
  856.       (.pv-cell. (pv-table-lookup-pv-args .pv-table. ,@pv-parameters))
  857.       (,pv (car .pv-cell.))
  858.       (,calls (cdr .pv-cell.)))
  859.      (declare ,(make-pv-type-declaration pv))
  860.      (declare ,(make-calls-type-declaration calls))
  861.      ,@(when (symbolp pv-table-symbol)
  862.      `((declare (special ,pv-table-symbol))))
  863.      ,pv ,calls
  864.      ,@forms))
  865.  
  866. (defvar *non-variable-declarations*
  867.   '(method-name method-lambda-list
  868.     optimize ftype inline notinline))
  869.  
  870. (defvar *variable-declarations-with-argument*
  871.   '(class
  872.     type))
  873.  
  874. (defvar *variable-declarations-without-argument*
  875.   '(ignore special dynamic-extent
  876.     array atom base-char bignum bit bit-vector character common compiled-function
  877.     complex cons double-float extended-char fixnum float function hash-table integer
  878.     keyword list long-float nil null number package pathname random-state ratio
  879.     rational readtable sequence short-float signed-byte simple-array
  880.     simple-bit-vector simple-string simple-vector single-float standard-char
  881.     stream string-char symbol t unsigned-byte vector))
  882.  
  883. (defun split-declarations (body args)
  884.   (let ((inner-decls nil) (outer-decls nil) decl)
  885.     (loop (when (null body) (return nil))
  886.       (setq decl (car body))
  887.       (unless (and (consp decl)
  888.                (eq (car decl) 'declare))
  889.         (return nil))
  890.       (dolist (form (cdr decl))
  891.         (when (consp form)
  892.           (let ((declaration-name (car form)))
  893.         (if (member declaration-name *non-variable-declarations*)
  894.             (push `(declare ,form) outer-decls)
  895.             (let ((arg-p
  896.                (member declaration-name
  897.                    *variable-declarations-with-argument*))
  898.               (non-arg-p
  899.                (member declaration-name
  900.                    *variable-declarations-without-argument*))
  901.               (dname (list (pop form)))
  902.               (inners nil) (outers nil))
  903.               (unless (or arg-p non-arg-p)
  904.             (warn "The declaration ~S is not understood by ~S.~@
  905.                                Please put ~S on one of the lists ~S,~%~S, or~%~S.~@
  906.                         (Assuming it is a variable declarations without argument)."
  907.                   declaration-name 'split-declarations
  908.                   declaration-name
  909.                   '*non-variable-declarations*
  910.                   '*variable-declarations-with-argument*
  911.                   '*variable-declarations-without-argument*)
  912.             (push declaration-name
  913.                   *variable-declarations-without-argument*))
  914.               (when arg-p
  915.             (setq dname (append dname (list (pop form)))))
  916.               (dolist (var form)
  917.             (if (member var args)
  918.                 (push var outers)
  919.                 (push var inners)))
  920.               (when outers
  921.             (push `(declare (,@dname ,@outers)) outer-decls))
  922.               (when inners
  923.             (push `(declare (,@dname ,@inners)) inner-decls)))))))
  924.       (setq body (cdr body)))
  925.     (values outer-decls inner-decls body)))
  926.  
  927. (defun make-method-initargs-form-internal (method-lambda initargs env)
  928.   (declare (ignore env))
  929.   (let (method-lambda-args lmf lmf-params)
  930.     (if (not (and (= 3 (length method-lambda))
  931.           (= 2 (length (setq method-lambda-args (cadr method-lambda))))
  932.           (consp (setq lmf (third method-lambda)))
  933.           (eq 'simple-lexical-method-functions (car lmf))
  934.           (eq (car method-lambda-args) (cadr (setq lmf-params (cadr lmf))))
  935.           (eq (cadr method-lambda-args) (caddr lmf-params))))
  936.     `(list* :function #',method-lambda
  937.             ',initargs)
  938.     (let* ((lambda-list (car lmf-params))
  939.            (nreq 0)(restp nil)(args nil))
  940.       (dolist (arg lambda-list)
  941.         (when (member arg '(&optional &rest &key))
  942.           (setq restp t)(return nil))
  943.         (when (eq arg '&aux) (return nil))
  944.         (incf nreq)(push arg args))
  945.       (setq args (nreverse args))
  946.       (setf (getf (getf initargs ':plist) ':arg-info) (cons nreq restp))
  947.       (make-method-initargs-form-internal1
  948.        initargs (cddr lmf) args lmf-params restp)))))
  949.  
  950. (defun make-method-initargs-form-internal1 
  951.     (initargs body req-args lmf-params restp)
  952.   (multiple-value-bind (outer-decls inner-decls body)
  953.       (split-declarations body req-args)
  954.     (let* ((rest-arg (when restp '.rest-arg.))
  955.        (args+rest-arg (if restp (append req-args (list rest-arg)) req-args)))
  956.       `(list* :fast-function
  957.     #'(lambda (.pv-cell. .next-method-call. ,@args+rest-arg)
  958.         ,@outer-decls
  959.         .pv-cell. .next-method-call.
  960.         (macrolet ((pv-env ((pv calls pv-table-symbol pv-parameters)
  961.                 &rest forms)
  962.              (declare (ignore pv-table-symbol pv-parameters))
  963.              `(let ((,pv (car .pv-cell.))
  964.                 (,calls (cdr .pv-cell.)))
  965.                (declare ,(make-pv-type-declaration pv)
  966.                 ,(make-calls-type-declaration calls))
  967.                ,pv ,calls
  968.                ,@forms)))
  969.           (fast-lexical-method-functions 
  970.            (,(car lmf-params) .next-method-call. ,req-args ,rest-arg
  971.          ,@(cdddr lmf-params))
  972.            ,@inner-decls
  973.            ,@body)))
  974.     ',initargs))))
  975.  
  976. ;use arrays and hash tables and the fngen stuff to make this much better.
  977. ;It doesn't really matter, though, because a function returned by this
  978. ;will get called only when the user explicitly funcalls a result of method-function. 
  979. ;BUT, this is needed to make early methods work.
  980. (defun method-function-from-fast-function (fmf)
  981.   (declare (type function fmf))
  982.   (let* ((method-function nil) (pv-table nil)
  983.      (arg-info (method-function-get fmf ':arg-info))
  984.      (nreq (car arg-info))
  985.      (restp (cdr arg-info)))
  986.     (setq method-function
  987.       #'(lambda (method-args next-methods)
  988.           (unless pv-table
  989.         (setq pv-table (method-function-pv-table fmf)))
  990.           (let* ((pv-cell (when pv-table
  991.                 (get-method-function-pv-cell 
  992.                  method-function method-args pv-table)))
  993.              (nm (car next-methods))
  994.              (nms (cdr next-methods))
  995.              (nmc (when nm
  996.                 (make-method-call :function (if (std-instance-p nm)
  997.                                 (method-function nm)
  998.                                 nm)
  999.                           :call-method-args (list nms)))))
  1000.         (if restp
  1001.             (let* ((rest (nthcdr nreq method-args))
  1002.                (args (ldiff method-args rest)))
  1003.               (apply fmf pv-cell nmc (nconc args (list rest))))
  1004.             (apply fmf pv-cell nmc method-args)))))
  1005.     (let* ((fname (method-function-get fmf :name))
  1006.        (name `(,(or (get (car fname) 'method-sym)
  1007.             (setf (get (car fname) 'method-sym)
  1008.                   (let ((str (symbol-name (car fname))))
  1009.                 (if (string= "FAST-" str :end2 5)
  1010.                     (intern (subseq str 5) *the-pcl-package*)
  1011.                     (car fname)))))
  1012.             ,@(cdr fname))))
  1013.       (set-function-name method-function name))      
  1014.     (setf (method-function-get method-function :fast-function) fmf)
  1015.     method-function))
  1016.  
  1017. (defun get-method-function-pv-cell (method-function method-args &optional pv-table)
  1018.   (let ((pv-table (or pv-table (method-function-pv-table method-function))))
  1019.     (when pv-table
  1020.       (let ((pv-wrappers (pv-wrappers-from-all-args pv-table method-args)))
  1021.     (when pv-wrappers
  1022.       (pv-table-lookup pv-table pv-wrappers))))))
  1023.  
  1024. (defun pv-table-lookup-pv-args (pv-table &rest pv-parameters)
  1025.   (pv-table-lookup pv-table (pv-wrappers-from-pv-args pv-parameters)))
  1026.  
  1027. (defun pv-wrappers-from-pv-args (&rest args)
  1028.   (let* ((nkeys (length args))
  1029.      (pv-wrappers (make-list nkeys))
  1030.      w (w-t pv-wrappers))
  1031.     (dolist (arg args)
  1032.       (setq w (cond ((std-instance-p arg)
  1033.              (std-instance-wrapper arg))
  1034.             ((fsc-instance-p arg)
  1035.              (fsc-instance-wrapper arg))
  1036.             (t
  1037.              #+new-kcl-wrapper
  1038.              (built-in-wrapper-of arg)
  1039.              #-new-kcl-wrapper
  1040.              (built-in-or-structure-wrapper arg))))
  1041.       (unless (eq 't (wrapper-state w))
  1042.     (setq w (check-wrapper-validity arg)))
  1043.       (setf (car w-t) w))
  1044.       (setq w-t (cdr w-t))
  1045.       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
  1046.       pv-wrappers))
  1047.  
  1048. (defun pv-wrappers-from-all-args (pv-table args)
  1049.   (let ((nkeys 0)
  1050.     (slot-name-lists (pv-table-slot-name-lists pv-table)))
  1051.     (dolist (sn slot-name-lists)
  1052.       (when sn (incf nkeys)))
  1053.     (let* ((pv-wrappers (make-list nkeys))
  1054.        (pv-w-t pv-wrappers))
  1055.       (dolist (sn slot-name-lists)
  1056.     (when sn
  1057.       (let* ((arg (car args))
  1058.          (w (wrapper-of arg)))
  1059.         (unless w ; can-optimize-access prevents this from happening.
  1060.           (error "error in pv-wrappers-from-all-args"))
  1061.         (setf (car pv-w-t) w)
  1062.         (setq pv-w-t (cdr pv-w-t))))
  1063.     (setq args (cdr args)))
  1064.       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
  1065.       pv-wrappers)))
  1066.  
  1067. (defun pv-wrappers-from-all-wrappers (pv-table wrappers)
  1068.   (let ((nkeys 0)
  1069.     (slot-name-lists (pv-table-slot-name-lists pv-table)))
  1070.     (dolist (sn slot-name-lists)
  1071.       (when sn (incf nkeys)))
  1072.     (let* ((pv-wrappers (make-list nkeys))
  1073.        (pv-w-t pv-wrappers))
  1074.       (dolist (sn slot-name-lists)
  1075.     (when sn
  1076.       (let ((w (car wrappers)))
  1077.         (unless w ; can-optimize-access prevents this from happening.
  1078.           (error "error in pv-wrappers-from-all-wrappers"))
  1079.         (setf (car pv-w-t) w)
  1080.         (setq pv-w-t (cdr pv-w-t))))
  1081.     (setq wrappers (cdr wrappers)))
  1082.       (when (= nkeys 1) (setq pv-wrappers (car pv-wrappers)))
  1083.       pv-wrappers)))
  1084.